package cn.itcast.bitmapdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class MyView extends View {
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //this.setBackgroundResource(R.drawable.bg);

        canvas.save();
        Matrix matrix = new Matrix();

        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(100, 100);
        path.lineTo(100, 400);
        path.lineTo(0, 500);
        path.lineTo(0, 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            canvas.clipPath(path);
            canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bg), matrix, new Paint());
        }

        canvas.restore();
        canvas.save();

        path = new Path();
        path.moveTo(0, 0);
        path.lineTo(400, 0);
        path.lineTo(300, 100);
        path.lineTo(100, 100);
        path.lineTo(0, 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            canvas.clipPath(path);
            canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bg2), matrix, new Paint());
        }

        canvas.restore();
        canvas.save();

        path = new Path();
        path.moveTo(400, 0);
        path.lineTo(400, 500);
        path.lineTo(300, 400);
        path.lineTo(300, 100);
        path.lineTo(400, 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            canvas.clipPath(path);
            canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bg), matrix, new Paint());
        }

        canvas.restore();
        canvas.save();

        path = new Path();
        path.moveTo(400, 500);
        path.lineTo(0, 500);
        path.lineTo(100, 400);
        path.lineTo(300, 400);
        path.lineTo(400, 500);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            canvas.clipPath(path);
            canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bg2), matrix, new Paint());
        }
        canvas.restore();
        canvas.save();


        matrix.postTranslate(100, 100);
        Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.car);
        Bitmap bitmap = Bitmap.createScaledBitmap(src,200,300,false);
        canvas.drawBitmap(bitmap, matrix, new Paint());


    }
}
